package com.th.basemodel.ui

import android.content.Context
import android.text.TextPaint
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.th.basemodel.R

class TextKUnderLine @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : AppCompatTextView(context, attrs, defStyleAttr) {

    private lateinit var _textPaint: TextPaint//TextPaint对象，继承自Paint
    private var _enabled = false//是否加下划线

    init {
        initAttrs(attrs, defStyleAttr)
        initPaint()
    }

     fun initAttrs(attrs: AttributeSet?, defStyleAttr: Int) {
        attrs ?: return
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextKUnderLine, defStyleAttr, 0)//Load attributes 加载属性列表R.styleable.UnderLineTextView
        _enabled =
            typedArray.getBoolean(R.styleable.TextKUnderLine_textKUnderLine_enabled, false)        //获取自定义属性,默认是false
        typedArray.recycle()
    }

     fun initPaint() {
        _textPaint = paint
        if (_enabled) {
            //设置下划线
            _textPaint.isUnderlineText = true
        }
    }
}
